package com.yd.location.module.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yd.location.auth.domain.User;
import com.yd.location.auth.service.UserService;
import com.yd.location.common.CommonConstant;
import com.yd.location.common.annotation.DeleteUrl;
import com.yd.location.common.annotation.ListUrl;
import com.yd.location.common.annotation.SaveUrl;
import com.yd.location.common.annotation.UpdateUrl;
import com.yd.location.common.cache.RedisKeyEnum;
import com.yd.location.common.cache.RedisUtil;
import com.yd.location.common.spring.mpbase.IdEntity;
import com.yd.location.common.spring.mpbase.MyController;
import com.yd.location.common.spring.mpbase.MyService;
import com.yd.location.common.spring.pagination.PageRequestDTO;
import com.yd.location.common.web.ParamUtil;
import com.yd.location.common.web.Resp;
import com.yd.location.module.cms.domain.SystemNotice;
import com.yd.location.module.cms.domain.UserSystemNotice;
import com.yd.location.module.cms.mapper.SystemNoticeMapper;
import com.yd.location.module.cms.service.SystemNoticeService;
import com.yd.location.module.cms.service.UserSystemNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Created by zk on 2020-07-14 20:57
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/cms/system_notice")
public class SystemNoticeController extends MyController<SystemNoticeMapper, SystemNotice, PageRequestDTO> {
    @Resource
    private SystemNoticeService systemNoticeService;
    @Resource
    private UserService userService;
    @Resource
    private UserSystemNoticeService userSystemNoticeService;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public MyService<SystemNoticeMapper, SystemNotice> getService() {
        return systemNoticeService;
    }

    @ListUrl
    @Override
    public Resp list(PageRequestDTO condition) {
        return super.list(condition);
    }

    @SaveUrl
    @Override
    public Resp save(SystemNotice entity) {
        super.save(entity);
        List<User> list = userService.lambdaQuery()
                .select(IdEntity::getId)
                .eq(User::getType, CommonConstant.USER_ISSUER)
                .list();
        if (!ParamUtil.isBlank(list)) {
            List<UserSystemNotice> userSystemNotices = new LinkedList<>();
            for (User user : list) {
                UserSystemNotice userSystemNotice = new UserSystemNotice();
                userSystemNotice.setUserId(user.getId());
                userSystemNotice.setSystemNoticeId(entity.getId());
                userSystemNotices.add(userSystemNotice);
            }
            if (!ParamUtil.isBlank(userSystemNotices)) {
                userSystemNoticeService.saveBatch(userSystemNotices);
            }
        }
        return Resp.success("添加成功");
    }

    @UpdateUrl
    @Override
    public Resp update(SystemNotice entity) {
        super.update(entity);
        redisUtil.delete(RedisKeyEnum.SYSTEM_NOTICE, entity.getId());
        return Resp.success("修改成功");
    }

    @DeleteUrl
    @Override
    public Resp delete(String[] ids) {
        super.delete(ids);
        QueryWrapper<UserSystemNotice> wrapper;
        for (String id : ids) {
            redisUtil.delete(RedisKeyEnum.SYSTEM_NOTICE, id);
            wrapper = new QueryWrapper<>();
            wrapper.eq("system_notice_id", id);
            userSystemNoticeService.remove(wrapper);
        }
        return Resp.success("删除成功");
    }
}
